#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:Summerday

# 从数据库中获取数据 写入csv
import sqlite3
import csv
cx = sqlite3.connect("D:\\comp\\tdzl\\CBDB_20200528.db")

sql = """
select b.c_personid sourceId ,b.c_name_chn sourceName,AT.c_assoc_type_desc_chn assocType,AD.c_assoc_id targetId,ac.c_assoc_desc_chn assocDesc from BIOG_MAIN b
    left  join ASSOC_DATA AD on b.c_personid = AD.c_personid
    left join ASSOC_CODES ac on ac.c_assoc_code = AD.c_assoc_code
    left join ASSOC_CODE_TYPE_REL ACTR on ac.c_assoc_code = ACTR.c_assoc_code
    left join ASSOC_TYPES AT on AT.c_assoc_type_id = ACTR.c_assoc_type_id
where b.c_birthyear
    between 1368 and 1644
  and b.c_death_age!=0
and AT.c_assoc_type_parent_id = '02';
"""

data = cx.execute(sql)
data_list = data.fetchall()


with open('csv/edges.csv','w',encoding='utf-8',newline='') as f:
	w = csv.writer(f)
	w.writerow(['sourceId','sourceName','assocType','targetId','assocDesc'])
	for d in data_list:
		w.writerow(list(d))
		
